//
//  BJGuidePageView.m
//  ZWGuidePage
//
//  Created by lishen Pro on 2022/9/27.
//

#import "BJGuidePageView.h"
#import "BJLoginViewController.h"

#define kWidth [UIScreen mainScreen].bounds.size.width
#define kHeight [UIScreen mainScreen].bounds.size.height
#define img(imageName) [UIImage imageNamed:imageName]

@interface BJGuidePageView () <UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UIPageControl *pageControl;
@property (nonatomic, strong) NSArray *titleArr;
@property (nonatomic, strong) NSArray *contentArr;
@property (nonatomic, strong) NSTimer *timer;

@end

@implementation BJGuidePageView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.scrollView];
        [self addSubview:self.pageControl];
        self.imageArr = @[@1];


    }
    return self;
}

#pragma mark - delegate

/* 已经停止加速 */
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    self.pageControl.currentPage = self.scrollView.contentOffset.x / kWidth;
    if (self.scrollView.contentOffset.x / kWidth == 3) {
        [self.timer setFireDate:[NSDate distantFuture]];
        [self.timer invalidate];
        self.timer = nil;
        if (self.toNextVC) {
            self.toNextVC();
        }
    }
    [self.timer setFireDate:[NSDate dateWithTimeIntervalSinceNow:self.duration]];

    
}

#pragma mark - getter setter方法

- (void)setImageArr:(NSArray *)imageArr {
    self.titleArr = @[@"Join",@"Archery",@"Welcome"];
    self.contentArr = @[@"our archery community! Share, learn, connect.",@"lovers unite! Share, connect, aim for greatness.",@"to the archery hub! Share your shots, connect with enthusiasts."];
    for (int i = 0; i < 3; i++) {
        UIImageView *view = [[UIImageView alloc] initWithFrame:CGRectMake(i * kWidth, 0, kWidth, kHeight)];
        NSString *str = [NSString stringWithFormat:@"icon_guide%d.png", i];
        view.image = img(str);
        [self.scrollView addSubview:view];
        

        
        UILabel *contentL = [[UILabel alloc]init];
        contentL.text = self.contentArr[i];
        contentL.font = [UIFont systemFontOfSize:17];
        contentL.numberOfLines = 0;
        contentL.textColor = [UIColor colorWithHexString:@"#6DEAFD"];
        [view addSubview:contentL];
        contentL.makeCons(^{
            make.left.constants(10);
            make.right.constants(-2);
            make.bottom.constants(-84);
        });
        
        UILabel *titleL = [[UILabel alloc]init];
        titleL.text = self.titleArr[i];
        titleL.font = [UIFont boldSystemFontOfSize:24];
        //our archery community! Share, learn, connect.
        titleL.textColor = [UIColor colorWithHexString:@"#6DEAFD"];
        [view addSubview:titleL];
        titleL.makeCons(^{
            make.left.constants(10);
            make.right.constants(-10);
            make.bottom.view(contentL).top.constants(-3);
        });
        
        
    }
}

- (void)setDuration:(NSTimeInterval)duration
{
    _duration = duration;
    if (duration > 0.0) {
//        NSLog(@"%f", duration);
        
        self.timer = [NSTimer scheduledTimerWithTimeInterval:duration target:self selector:@selector(changeNext) userInfo:nil repeats:YES];
        [self.timer setFireDate:[NSDate dateWithTimeIntervalSinceNow:duration]];
    }
}
- (void)changeNext
{
    if (self.scrollView.contentOffset.x / kWidth == 2) {
        [self.timer setFireDate:[NSDate distantFuture]];
        [self.timer invalidate];
        self.timer = nil;
        if (self.toNextVC) {
            self.toNextVC();
        }
    }

    self.pageControl.currentPage = self.scrollView.contentOffset.x / kWidth +1;
    [self.scrollView setContentOffset:CGPointMake(self.pageControl.currentPage*CGRectGetWidth(self.scrollView.bounds), 0) animated:YES];

}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.timer setFireDate:[NSDate distantFuture]];
}






#pragma mark - 懒加载

- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kWidth, kHeight)];
        _scrollView.contentSize = CGSizeMake(kWidth * 4, kHeight);
        _scrollView.pagingEnabled = YES;
        _scrollView.delegate = self;
    }
    return _scrollView;
}

- (UIPageControl *)pageControl {
    if (!_pageControl) {
        _pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, kHeight - 50, kWidth, 10)];
        _pageControl.numberOfPages = 3;
        _pageControl.currentPageIndicatorTintColor = [UIColor colorWithRed:0.153 green:0.533 blue:0.796 alpha:1.0];
    }
    return _pageControl;
}

@end
